#include "file_content_servlet.h"
#include <stdio.h>

namespace vmon_web
{
    FileContentServlet::FileContentServlet(const string& rootDir):
        m_rootDir(rootDir)
    {
        printf("FileContentServlet: constructed\n");
    }

    FileContentServlet::~FileContentServlet()
    {
    }

    bool FileContentServlet::handleRequest(HttpRequest& request, HttpResponse& response)
    {
        // get file name
        string path = request.getPath();
        string filePath = m_rootDir + path;
        FILE* file = fopen(filePath.c_str(), "r");
        printf("FileContentServlet: handling path %s\n", filePath.c_str());
        if (file != 0)
        {
            char c;
            response.sendHead("200 OK");
            while (EOF != (c = fgetc(file)))
            {
                response.send(&c, sizeof(c));
            }
            fclose(file);
        }
        // we can close the connection
        return false;
    }
}
